import { MipmapGenerator } from './mipmapGenerator';
 
export class CachedMipmapGenerator extends MipmapGenerator {
    private static cache = new Map<string, MipmapGenerator>();
 
    public static generate(
        imageData: Uint8ClampedArray,
        width: number,
        height: number
    ): MipmapGenerator {
        const key = `${width}x${height}-${this.hashData(imageData)}`;
        
        if (this.cache.has(key)) {
            return this.cache.get(key)!;
        }
 
        const generator = new MipmapGenerator(imageData, width, height);
        this.cache.set(key, generator);
        return generator;
    }
 
    private static hashData(data: Uint8ClampedArray): string {
        // 使用更高效的哈希算法（示例使用简化版）
        let hash = 0;
        for (let i = 0; i < data.length; i++) {
            hash = (hash << 5) - hash + data[i];
            hash |= 0; // 转换为32位整数
        }
        return hash.toString(16);
    }
}